﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using MpSoft.Library.BUSS.Settings;
using MpSoft.Library.DTO;
using CKEditor.NET;

namespace MpSoft.Web.AdminCP.Controls.Configs
{
    public partial class SettingEdit : System.Web.UI.UserControl
    {
        #region Var

        private int ItemID = -1;
        private Setting setting = null;

        #endregion

        #region Method

        private void BindControl()
        {
            if (ItemID > 0)
            {
                if (setting == null)
                    setting = SettingBUSS.Setting_ByID(ItemID);
                if (setting != null)
                {
                    //txtValue.Text = obj.SettingValue;
                    switch (TypeControl)
                    {
                        case "editor":
                            CKEditorControl editor = new CKEditorControl();
                            editor.ID = "editor";
                            editor.Text = setting.SettingValue;
                            plControl.Controls.Add(editor);
                            break;
                        default:
                            TextBox textbox = new TextBox();
                            textbox.Text = setting.SettingValue;
                            textbox.ID = "textbox";
                            textbox.CssClass = "form-control";
                            textbox.TextMode = TextBoxMode.MultiLine;
                            textbox.Height = 250;
                            plControl.Controls.Add(textbox);
                            break;
                    }
                }
                   
            }
        }

        private void Message(string message)
        {
            ltlMessage.Text = "<p class='message-success'><i class='glyphicon-ok'></i> " + message + "</p>";
        }

        protected string GetUrl(string typeControl)
        {
            return "?mod=settingedit&itemid=" + ItemID + "&control=" + typeControl;
        }

        #endregion

        #region Event

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["itemid"] != null)
                int.TryParse(Request.QueryString["itemid"].ToString(), out ItemID);
            //if (!IsPostBack)
                BindControl();
        }

        protected void lnkSave_Click(object sender, EventArgs e)
        {
            if (ItemID > 0)
            {
                if (setting == null)
                    setting = SettingBUSS.Setting_ByID(ItemID);
                if (setting != null)
                {
                    //obj.SettingValue = txtValue.Text;
                    switch (TypeControl)
                    {
                        case "editor":
                            var editor = plControl.FindControl("editor") as CKEditorControl;
                            if (editor != null)
                                setting.SettingValue = editor.Text;
                            break;
                        default:
                            var textbox = plControl.FindControl("textbox") as TextBox;
                            if (textbox != null)
                                setting.SettingValue = textbox.Text;
                            break;
                    }
                }

                if (SettingBUSS.Setting_Update(setting) > 0)
                    Message("Cập nhật thành công!");
            }
        }

        protected void lnkSaveExit_Click(object sender, EventArgs e)
        {
            if (ItemID > 0)
            {
                if (setting == null)
                    setting = SettingBUSS.Setting_ByID(ItemID);
                switch (TypeControl)
                {
                    case "editor":
                        var editor = plControl.FindControl("editor") as CKEditorControl;
                        if (editor != null)
                            setting.SettingValue = editor.Text;
                        break;
                    default:
                        var textbox = plControl.FindControl("textbox") as TextBox;
                        if (textbox != null)
                            setting.SettingValue = textbox.Text;
                        break;
                }
                if (SettingBUSS.Setting_Update(setting) > 0)
                    Response.Redirect("?mod=setting");
            }
        }

        #endregion

        #region Properties

        public string TypeControl
        {
            get
            {
                if (setting == null)
                    setting = SettingBUSS.Setting_ByID(ItemID);
                if (Request.QueryString["control"] != null)
                {
                    if (setting.SettingType == 1)
                        return "textbox";
                    return Request.QueryString["control"].ToString();
                }
                if (setting.SettingType == 2)
                    return "editor";
                return "textbox";
            }
        }

        #endregion
    }
}